package com.ruoyi.system.manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.DataRecordFlag;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysStudent;
import com.ruoyi.system.manager.SysStudentManager;
import com.ruoyi.system.mapper.SysStudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author jlc
 * @date 2022/9/4
 */
@Slf4j
@Service
public class SysStudentManagerImpl extends ServiceImpl<SysStudentMapper, SysStudent> implements SysStudentManager {
    /**
     * 通过身份证号查询人
     *
     * @param idCard
     * @return
     */
    @Override
    public SysStudent getByIdCard(String idCard) {
        LambdaQueryWrapper<SysStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(idCard), SysStudent::getIdCard, idCard)
                .eq(SysStudent::getDelFlag, DataRecordFlag.NORMAL.getCode());

        List<SysStudent> list = list(queryWrapper);
        if (list.size() > 1) {
            log.error("数据查询到多条，数据异常及时修复");
            return list.get(0);
        }
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 根据学生信息获取列表
     *
     * @param user
     * @return
     */
    @Override
    public List<SysStudent> queryList(SysStudent user) {
        LambdaQueryWrapper<SysStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(SysStudent::getDelFlag, DataRecordFlag.NORMAL.getCode())
                .eq(StringUtils.isNotEmpty(user.getIdCard()), SysStudent::getIdCard, user.getIdCard())
                .like(StringUtils.isNotEmpty(user.getName()), SysStudent::getName, user.getName())
                .eq(StringUtils.isNotEmpty(user.getStatus()), SysStudent::getStatus, user.getStatus())
                .like(StringUtils.isNotEmpty(user.getPhone()), SysStudent::getPhone, user.getPhone())
                .like(StringUtils.isNotEmpty(user.getQqNumber()), SysStudent::getQqNumber, user.getQqNumber())
                .eq(StringUtils.isNotEmpty(user.getSex()), SysStudent::getSex, user.getSex())
                .eq(user.getDeptId() != null, SysStudent::getDeptId, user.getDeptId())
                .eq(StringUtils.isNotEmpty(user.getGroupName()), SysStudent::getGroupName, user.getGroupName());
        return list(queryWrapper);
    }

}
